< previous page page_672 next page >

Page 672
Chapter Objectives
Learn some speed optimization tricks
Learn to time code to see which version runs faster
Learn some size optimization tricks
Sample Project File: \Chapter33\Optimization.vbp
Speed Optimization Tricks
Optimizing for speed is probably the most important thing you can do for your applications. Because a graphical user interface such as Windows will always be a little slower than a text-based application, the more tricks you can use to speed up your program, the better your users will like it. In this chapter, youll load the sample project and run each of the tests to see whether the premises you thought were true are really valid assumptions.
Examples Filename: Speed.frm
In each of the examples run, youll see a routine that creates an instance of a TimerCompare class. This class uses the timeGetTime() Windows API call to perform measurements in milliseconds. It tracks two sets of timesone for the slow method and one for the fast method. You should always perform what you think will be the slow method first and then perform the fast method. Each time, you invoke the Startn method, perform the process, and then invoke the Quitn method. After both processes have run, check the Percent property in the TimerCompare object to have it return the percentage difference between the two times. Heres the code for the sample application:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub LongVSVariant()
   Dim lngLoop As Long
   Dim vntValue As Variant
   Dim lngValue As Long
   Dim oTimer As TimerCompare

   ' Create new Timer object
   Set oTimer = New TimerCompare
   ' Start timer for "Slow" method
   oTimer.Start1
   ' Perform the "Slow" method

 
< previous page page_672 next page >

If you like this book, buy it!